package org.soenea.doitall.domain.model.user.data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.dsrg.soenea.service.threadLocal.DbRegistry;

public class UserFinder {
	public static final String BASE_NAME = "user";
	public static final String TABLE = DbRegistry.getTablePrefix()+BASE_NAME;

	public static final String SELECT_BY_CREDENTIALS = "SELECT p.userID, p.version, p.userName, p.firstName, p.lastName, p.password, p.groupID, p.admin FROM " + TABLE + " AS p WHERE p.userName=? AND p.password=?;";
	public static final String SELECT_BY_ID = "SELECT p.userID, p.version, p.userName, p.firstName, p.lastName, p.password, p.groupID, p.admin FROM " + TABLE + " AS p WHERE p.userID=?;";
	public static final String SELECT_BY_USERNAME = "SELECT p.userID, p.version, p.userName, p.firstName, p.lastName, p.password, p.groupID, p.admin FROM " + TABLE + " AS p WHERE p.userName=?;";
	public static final String SELECT_BY_GROUP_ID = "SELECT p.userID, p.version, p.userName, p.firstName, p.lastName, p.password, p.groupID, p.admin FROM " + TABLE + " AS p WHERE p.groupID = ?;";
	
	public static ResultSet findByCredentials(String userName, String password) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT_BY_CREDENTIALS);
		ps.setString(1, userName);
		ps.setString(2, password);
		return ps.executeQuery(); 
	}

	public static ResultSet find(long userID) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT_BY_ID);
		ps.setLong(1, userID);
		return ps.executeQuery();
	}
	
	public static ResultSet find(String userName) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT_BY_USERNAME);
		ps.setString(1, userName);
		return ps.executeQuery();
	}
	
	public static ResultSet findByGroupID(long groupID) throws SQLException {
		PreparedStatement ps = DbRegistry.getDbConnection().prepareStatement(SELECT_BY_GROUP_ID);
		ps.setLong(1, groupID);
		return ps.executeQuery();
	}
}
